# # 解决tpshop商城登录“验证码错误”的问题
# # 导包
# import requests
# # 发送请求
# # 发送验证码接口的请求，获取验证码接口返回的cookie给登录接口使用
# resp1 = requests.get(url="http://127.0.0.1/index.php?m=Home&c=User&a=verify&"
#                          "r=0.2958557703476097 ")
# # 获取验证码接口返回的cookie
# my_cookie = resp1.cookies
# # 发送登录请求
# resp2 = requests.post(url="http://127.0.0.1/index.php?m=Home&c=User&a=do_login&t=0.5251530912658221",
#               headers={"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},
#               data={"username":"13800000000","password":"123456","verify_code":"8888"},
#               cookies=my_cookie)
# # 查看结果
# print(resp2.json())

# 导包
import requests
# 创建session对象
sess = requests.Session()   #不要忘记括号（）
# 使用session对象发送获取验证码的请求
resp1 = sess.get(url="http://127.0.0.1/index.php?m=Home&c=User&a=verify&r=0.2958557703476097")
# 使用同一个session对象发送登录请求
resp2 = sess.post(url="http://127.0.0.1/index.php?m=Home&c=User&a=do_login&t=0.5251530912658221",
              headers={"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},
              data={"username":"13800000000","password":"123456","verify_code":"8888"})
# 查看响应结果
print(resp2.json())
# print(resp2.text)
print(resp2.status_code)
print(resp2.url)
# print(resp2.headers)
print(resp2.cookies)
print(resp2.encoding)
